﻿/// <reference path="_namespace.js" />
/// <reference path="GameComponent.js" />
/// <reference path="IDrawable.js" />
/// <reference path="graphics/IGraphicsDeviceService.js"/>
/// <reference path="graphics/GraphicsDevice.js"/>
/// <reference path="GameServiceContainer.js"/>

/*****************************************************************************
Ajaxna library class definition. Inheriting from ajax.xna.framework.GameComponent.
Author: krobinson 10/14/2008 4:19:57 PM.
Class:  ajax.xna.framework.DrawableGameComponent
*****************************************************************************/

$addNamespace("ajax.xna.framework");
$imports("ajax.xna.framework.graphics.IGraphicsDeviceService");
$imports("ajax.xna.framework.graphics.GraphicsDevice");
$imports("ajax.xna.framework.GameServiceContainer");
//-----------------------------------------------------------------------------

ajax.xna.framework.DrawableGameComponent = function(game)
{
    ajax.xna.framework.DrawableGameComponent.superConstructor.apply(this, arguments);
    this._visible = true;
    this._drawOrder = 0;
    this.graphicsDevice = null;
    // Events
    this.getEvent(this.getWindow(), "onVisibleChanged");
    this.getEvent(this.getWindow(), "onDrawOrderChanged");
}
$extend("ajax.xna.framework.DrawableGameComponent", "ajax.xna.framework.GameComponent");
$supports("ajax.xna.framework.DrawableGameComponent", "ajax.xna.framework.IDrawable");
if ($getDesignMode())
    ajax.xna.framework.DrawableGameComponent.prototype = ajax.xna.framework.GameComponent.prototype;
//-----------------------------------------------------------------------------

// Members
//-----------------------------------------------------------------------------
ajax.xna.framework.DrawableGameComponent.prototype._deviceService = ajax.xna.framework.graphics.IGraphicsDeviceService.prototype;
ajax.xna.framework.DrawableGameComponent.prototype.graphicsDevice = ajax.xna.framework.graphics.GraphicsDevice.prototype;
ajax.xna.framework.DrawableGameComponent.prototype._visible = true;
ajax.xna.framework.DrawableGameComponent.prototype._drawOrder = 0;


// Public Methods
//-----------------------------------------------------------------------------
ajax.xna.framework.DrawableGameComponent.prototype.set_drawOrder = function(value)
{///<summary>The order in which to draw this object relative to other objects. Objects with lower values are drawn first.</summary>
 ///<param name="value" type="int" integer="true">Order in which to draw this object relative to other objects.</param>
    if (this._drawOrder != value)
    {
        this._drawOrder = value;
        this.onDrawOrderChanged.call(this);
    }
}
//-----------------------------------------------------------------------------
ajax.xna.framework.DrawableGameComponent.prototype.set_visible = function(value)
{///<summary>Indicates whether IDrawable.draw() should be called in Game.draw() for this game component.</summary>
 ///<param name="value" type="bool">true if draw() should be called; false otherwise.</param>
    if (this._visible != value)
    {
        this._visible = value;
        this.onVisibleChanged.call(this);
    }
}

//-----------------------------------------------------------------------------
ajax.xna.framework.DrawableGameComponent.prototype.loadContent = function()
{///<summary>Called when graphics resources need to be loaded. Override this method to load any component-specific graphics resources. No need to call base.</summary>
}
ajax.xna.framework.DrawableGameComponent.prototype.unloadContent = function()
{///<summary>Called when graphics resources need to be unloaded. Override this method to unload any component-specifc graphics resources. No need to call base.</summary>
}
//-----------------------------------------------------------------------------
ajax.xna.framework.DrawableGameComponent.prototype.initialize = function()
{///<summary>
    /// Called when the GameComponent needs to be initialized. Override this method
    /// to query for required services and load any non-graphics resources. Call base.
    ///</summary> 
    
    var services = ajax.xna.framework.GameServiceContainer.cast(this.get_game().services);
    var IGraphicsDeviceService = ajax.xna.framework.graphics.IGraphicsDeviceService;
    this._deviceService = IGraphicsDeviceService.cast(services.getService(IGraphicsDeviceService.getFunctionName()));
    if (this._deviceService)
    {
        this.graphicsDevice = this._deviceService.graphicsDevice;
        // Call Load Content as this is NOT part of the IDrawable interface and will NOT be called FOR us.    
        this.loadContent();
    }   
}

// Supports IDrawable
//-----------------------------------------------------------------------------
ajax.xna.framework.DrawableGameComponent.prototype.get_visible = function()
{///<summary>Indicates whether IDrawable.draw() should be called in Game.draw() for this game component.</summary>
 ///<returns type="bool">true if IDrawable.draw() should be called; false otherwise.</returns>
    return this._visible;
}
ajax.xna.framework.DrawableGameComponent.prototype.add_visibleChanged = function(handler)
{///<summary>Adds a handler to event raised when the IDrawable.get_visible() property changes.</summary>
    this.getEvent(this.getWindow(), "onVisibleChanged").addListener(handler);
}
ajax.xna.framework.DrawableGameComponent.prototype.remove_visibleChanged = function(handler)
{///<summary>Removes a handler to event raised when the IDrawable.get_visible() property changes.</summary>
    this.getEvent(this.getWindow(), "onVisibleChanged").removeListener(handler);
}
//-----------------------------------------------------------------------------

ajax.xna.framework.DrawableGameComponent.prototype.get_drawOrder = function()
{///<summary>The order in which to draw this object relative to other objects. Objects with lower values are drawn first.</summary>
 ///<returns type="int" integer="true">Order in which to draw this object relative to other objects.</returns>
    return this._drawOrder;
}
ajax.xna.framework.DrawableGameComponent.prototype.add_drawOrderChanged = function(handler)
{///<summary>Adds a handler to event raised when the IDrawable.get_drawOrder() property changes.</summary>
    this.getEvent(this.getWindow(), "onDrawOrderChanged").addListener(handler);
}
ajax.xna.framework.DrawableGameComponent.prototype.remove_drawOrderChanged = function(handler)
{///<summary>Removes a handler to event raised when the IDrawable.get_drawOrder() property changes.</summary>
    this.getEvent(this.getWindow(), "onDrawOrderChanged").removeListener(handler);
}
//-----------------------------------------------------------------------------


ajax.xna.framework.DrawableGameComponent.prototype.draw = function(gameTime)
{///<summary>Called when the DrawableGameComponent needs to be drawn. Override this method with component-specific drawing code. No need to call base.</summary>
 ///<param name="gameTime" type="ajax.drawing.Timer">Time passed since the last call to draw().</param>
}




// Statics
//-----------------------------------------------------------------------------
ajax.xna.framework.DrawableGameComponent.cast = function(obj)
{///<returns type="ajax.xna.framework.DrawableGameComponent"/>
    return obj;
}